package handler

import (
	"exam-day19/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Add(c *gin.Context) {
	shopName := c.PostForm("shopName")
	shopContent := c.PostForm("shopContent")

	shop := model.Shop{
		ShopName:    shopName,
		ShopContent: shopContent,
	}

	model.DB.Create(&shop)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": shop,
	})
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	var shop []model.Shop
	model.DB.Offset(offset).Limit(size).Find(&shop)

	var total int64
	model.DB.Model(&model.Shop{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  shop,
		"total": total,
	})

}
